package com.shouhuobao.take.wallet;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.CapitalHistoryBean;
import com.shouhuobao.take.wallet.adapter.CapitalHistoryAdapter;

import droid.frame.view.xlist.FooterListener;
import droid.frame.view.xlist.SListView;

/**
 * Created by joker on 2017/8/5.
 * 资金记录页面
 */
public class CapitalHistoryActivity extends BaseActivity2 implements View.OnClickListener {

    private RelativeLayout mChooseTypeRL;
    private TextView mChooseTypeTV;
    private TextView mTotalCountTV;//统计记录条数
    private SListView mListView;
    private CapitalHistoryAdapter mAdapter = null;
    private ReqResult<CapitalHistoryBean> result = null;
    private String type = "";//1.提现 11.寄件分成 12.短信充值

    private String totalHistoryStr = "共%1$d条记录";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().queryMyCapitalHistoryData(type, 1);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_wallet_capital_history);
        super.findViewById();
        getAppTitle().setCommonTitle("资金记录");
        //设置标题右边显示
        findViewById(getResources().getIdentifier("title_right_switcher", "id", getPackageName())).setVisibility(View.VISIBLE);
        int right = getResources().getIdentifier("title_right_text", "id", getPackageName());
        TextView rightTV = (TextView) findViewById(right);
        rightTV.setText("筛选");
        rightTV.setTextColor(getResources().getColor(R.color.app_font_green));
        rightTV.setOnClickListener(this);
        //选择分类布局信息
        mChooseTypeRL = (RelativeLayout) findViewById(R.id.choose_RL);
        mChooseTypeTV = (TextView) findViewById(R.id.choose_type_TV);
        findViewById(R.id.close_choose_IV).setOnClickListener(this);

        mTotalCountTV = (TextView) findViewById(R.id.history_total_TV);
        mListView = (SListView) findViewById(R.id.listview);
        mAdapter = new CapitalHistoryAdapter(null, getContext());
        mListView.setAdapter(mAdapter);

        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //如果有条件的话，就按条件来刷新，负责按照全部刷新
                AppHttp.getInstance().queryMyCapitalHistoryData(type, 1);
            }
        });
        mListView.setFooterListener(new FooterListener() {
            @Override
            public void onFooterVisible() {
                //如果有条件的话，就按条件来请求，负责按照全部请求
                if (result != null && result.getPageNum() < result.getTotalPage()) {
                    AppHttp.getInstance().queryMyCapitalHistoryData(type, result.getPageNum() + 1);
                }
            }
        });
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //筛选
            case R.id.title_right_text:
                View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_wallet_choose_type, null);
                PopupWindow popupWindow = DialogMgr.createPopupWindow(getContext(), view);
                popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
                popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                popupWindow.showAtLocation(findViewById(R.id.bootomV), Gravity.RIGHT | Gravity.BOTTOM, 0, 0);
                popupWindowView(view, popupWindow);
                break;
            //清空筛选条件
            case R.id.close_choose_IV:
                mChooseTypeRL.setVisibility(View.GONE);
                mChooseTypeTV.setText("");
                type = "";
                showLoadingDialog(null);
                AppHttp.getInstance().queryMyCapitalHistoryData(type, 1);
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.query_capital_history:
                cancelLoadingDialog();
                onRefreshComplete();
                result = JSON.parseList(msg.obj, CapitalHistoryBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result)) {
                            if (result.getPageNum() == 1) {
                                mAdapter.setItems(result.getResultList(), true);
                            } else {
                                mAdapter.appendItems(result.getResultList(), true);
                            }
                            mTotalCountTV.setText(String.format(totalHistoryStr, result.getTotal()));
                            mListView.updateFooter(result.getPageNum(), result.getTotalPage());
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });

                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    /**
     * 更新popupwindow状态和显示
     *
     * @param view
     * @param popupWindow
     */
    private void popupWindowView(View view, final PopupWindow popupWindow) {
        //关闭按钮
        ImageView closeIV = (ImageView) view.findViewById(R.id.title_right);
        //寄件分成
        TextView sendSeparateTV = (TextView) view.findViewById(R.id.send_separate_TV);
        //提现
        TextView cashTV = (TextView) view.findViewById(R.id.cash_TV);
        //短信消费
        TextView smsConsumeTV = (TextView) view.findViewById(R.id.sms_consume_TV);
        if (mChooseTypeRL.getVisibility() == View.GONE) {
            type = "";
            sendSeparateTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
            cashTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
            smsConsumeTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
            sendSeparateTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
            cashTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
            smsConsumeTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
        } else {
            if ("寄件分成".equals(mChooseTypeTV.getText().toString())) {
                type = "11";
                sendSeparateTV.setBackgroundResource(R.drawable.wallet_capital_select_on_shape);
                cashTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
                smsConsumeTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
                sendSeparateTV.setTextColor(getResources().getColor(R.color.app_font));
                cashTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
                smsConsumeTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
            } else if ("提现".equals(mChooseTypeTV.getText().toString())) {
                type = "1";
                sendSeparateTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
                cashTV.setBackgroundResource(R.drawable.wallet_capital_select_on_shape);
                smsConsumeTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
                sendSeparateTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
                cashTV.setTextColor(getResources().getColor(R.color.app_font));
                smsConsumeTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
            } else if ("短信消费".equals(mChooseTypeTV.getText().toString())) {
                type = "12";
                sendSeparateTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
                cashTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
                smsConsumeTV.setBackgroundResource(R.drawable.wallet_capital_select_on_shape);
                sendSeparateTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
                cashTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
                smsConsumeTV.setTextColor(getResources().getColor(R.color.app_font));
            } else {
                type = "";
                sendSeparateTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
                cashTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
                smsConsumeTV.setBackgroundResource(R.drawable.wallet_capital_select_off_shape);
                sendSeparateTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
                cashTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
                smsConsumeTV.setTextColor(getResources().getColor(R.color.app_fontcolor_2));
            }
        }
        sendSeparateTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果之前选中的和这次选中的一样，不作任何处理
                //和之前选择一样
                if (mChooseTypeRL.getVisibility() == View.VISIBLE && "寄件分成".equals(mChooseTypeTV.getText().toString())) {

                } else {
                    mChooseTypeRL.setVisibility(View.VISIBLE);
                    mChooseTypeTV.setText("寄件分成");
                    type = "11";
                    showLoadingDialog(null);
                    AppHttp.getInstance().queryMyCapitalHistoryData(type, 1);
                }
                popupWindow.dismiss();
            }
        });
        cashTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果之前选中的和这次选中的一样，不作任何处理
                //和之前选择一样
                if (mChooseTypeRL.getVisibility() == View.VISIBLE && "提现".equals(mChooseTypeTV.getText().toString())) {

                } else {
                    mChooseTypeRL.setVisibility(View.VISIBLE);
                    mChooseTypeTV.setText("提现");
                    type = "1";
                    showLoadingDialog(null);
                    AppHttp.getInstance().queryMyCapitalHistoryData(type, 1);
                }
                popupWindow.dismiss();
            }
        });
        smsConsumeTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果之前选中的和这次选中的一样，不作任何处理
                //和之前选择一样
                if (mChooseTypeRL.getVisibility() == View.VISIBLE && "短信消费".equals(mChooseTypeTV.getText().toString())) {

                } else {
                    mChooseTypeRL.setVisibility(View.VISIBLE);
                    mChooseTypeTV.setText("短信消费");
                    type = "12";
                    showLoadingDialog(null);
                    AppHttp.getInstance().queryMyCapitalHistoryData(type, 1);
                }
                popupWindow.dismiss();
            }
        });
        closeIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }
}
